package com.dries.portal.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dries.portal.admin.bo.AdminUserDetails;
import com.dries.portal.admin.dto.UmsAdminParam;
import com.dries.portal.admin.entity.UmsAdmin;
import com.dries.portal.admin.entity.UmsResource;
import com.dries.portal.admin.mapper.UmsAdminMapper;
import com.dries.portal.admin.mapper.UmsAdminRoleRelationMapper;
import com.dries.portal.admin.service.IUmsAdminCacheService;
import com.dries.portal.admin.service.IUmsAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements IUmsAdminService {

    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private IUmsAdminCacheService adminCacheService;
    @Resource
    private UmsAdminRoleRelationMapper adminRoleRelationMapper;

    /**
     * 根据用户名查找用户
     *
     * @param username
     * @return
     */
    @Override
    public UmsAdmin getAdminByUserName(String username) {
        UmsAdmin admin = adminCacheService.getAdmin(username);
        if(admin!=null) return  admin;
        admin = baseMapper.selectOne(new QueryWrapper<UmsAdmin>().lambda()
                .eq(UmsAdmin::getUsername, username)
        );
        if (admin != null) {
            adminCacheService.setAdmin(admin);
            return admin;
        }

        return null;
    }

    /**
     * 获取用户信息
     *
     * @param userId
     */
    @Override
    public AdminUserDetails loadUserByUsername(String userId) {
        // 获取用户信息
        UmsAdmin admin = getAdminByUserName(userId);
        if (admin != null) {
            List<UmsResource> resourceList = getResourceList(admin.getId());
            return new AdminUserDetails(admin, resourceList);
        }
        throw new UsernameNotFoundException("用户名或密码错误");
    }

    /**
     * 注册功能
     *
     * @param umsAdminParam
     */
    @Override
    public UmsAdmin register(UmsAdminParam umsAdminParam) {
        UmsAdmin umsAdmin = new UmsAdmin();
        BeanUtils.copyProperties(umsAdminParam, umsAdmin);
        umsAdmin.setCreateTime(new Date());
        umsAdmin.setStatus(1);
        // 查询是否有相同用户名的用户
        List<UmsAdmin> umsAdminList = baseMapper.selectList(new QueryWrapper<UmsAdmin>().lambda()
                .eq(UmsAdmin::getUsername, umsAdmin.getUsername())
        );
        if (umsAdminList.size() > 0) {
            return null;
        }
        // 将密码进行加密操作
        String encodePassword = passwordEncoder.encode(umsAdmin.getPassword());
        umsAdmin.setPassword(encodePassword);
        baseMapper.insert(umsAdmin);
        return umsAdmin;
    }

    @Override
    public List<UmsResource> getResourceList(Long adminId) {
        List<UmsResource> resourceList = adminCacheService.getResourceList(adminId);
        if(CollUtil.isNotEmpty(resourceList)){
            return  resourceList;
        }
        resourceList = adminRoleRelationMapper.getResourceList(adminId);
        if(CollUtil.isNotEmpty(resourceList)){
            adminCacheService.setResourceList(adminId,resourceList);
        }
        return resourceList;
    }
}